<html>
<head><meta charset="utf-8"><title>Promotion of fallible operations (division, indexing) · t-lang · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/index.html">t-lang</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Promotion.20of.20fallible.20operations.20(division.2C.20indexing).html">Promotion of fallible operations (division, indexing)</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="222982674"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Promotion%20of%20fallible%20operations%20%28division%2C%20indexing%29/near/222982674" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Promotion.20of.20fallible.20operations.20(division.2C.20indexing).html#222982674">(Jan 16 2021 at 14:02)</a>:</h4>
<p><a href="https://github.com/rust-lang/rust/pull/80579">https://github.com/rust-lang/rust/pull/80579</a> implements part of <a href="https://github.com/rust-lang/rfcs/blob/master/text/3027-infallible-promotion.md">RFC 3027</a> by no longer promoting division and indexing when it can fail. Can someone start rfcbot FCP for this?</p>
<p>The one open question raised in the PR is whether we should avoid "looking into" the constants of other crates. Changing <code>const</code> to a different value is already a breaking change in various ways; this would add yet another one.</p>



<a name="222990449"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Promotion%20of%20fallible%20operations%20%28division%2C%20indexing%29/near/222990449" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Promotion.20of.20fallible.20operations.20(division.2C.20indexing).html#222990449">(Jan 16 2021 at 16:25)</a>:</h4>
<p>what's the reasoning for forbidding potentially failing divisions in promoteds instead of moving the check outside of it?</p>



<a name="222990470"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Promotion%20of%20fallible%20operations%20%28division%2C%20indexing%29/near/222990470" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Promotion.20of.20fallible.20operations.20(division.2C.20indexing).html#222990470">(Jan 16 2021 at 16:25)</a>:</h4>
<p>we are already doing something similar for other fallible operations, e.g. <code>CheckedAdd</code></p>



<a name="222990544"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Promotion%20of%20fallible%20operations%20%28division%2C%20indexing%29/near/222990544" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Promotion.20of.20fallible.20operations.20(division.2C.20indexing).html#222990544">(Jan 16 2021 at 16:27)</a>:</h4>
<p>and it will be surprising that changing code from <code>CONST/ 8</code> to <code>CONST / (1 &lt;&lt; 3)</code> causes a lifetime issue, even if this will very rarely happen</p>



<a name="222990965"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Promotion%20of%20fallible%20operations%20%28division%2C%20indexing%29/near/222990965" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Promotion.20of.20fallible.20operations.20(division.2C.20indexing).html#222990965">(Jan 16 2021 at 16:34)</a>:</h4>
<p><span class="user-mention silent" data-user-id="216206">lcnr</span> <a href="#narrow/stream/213817-t-lang/topic/Promotion.20of.20fallible.20operations.20(division.2C.20indexing)/near/222990470">said</a>:</p>
<blockquote>
<p>we are already doing something similar for other fallible operations, e.g. <code>CheckedAdd</code></p>
</blockquote>
<p>in MIR, Addition (and its failure on overflow) are very different from division and its failure on 0</p>



<a name="222990985"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Promotion%20of%20fallible%20operations%20%28division%2C%20indexing%29/near/222990985" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Promotion.20of.20fallible.20operations.20(division.2C.20indexing).html#222990985">(Jan 16 2021 at 16:35)</a>:</h4>
<p><code>Add</code> and <code>CheckedAdd</code> are both total (they never fail), one just returns a <code>bool</code> indicating whether an overflow happens</p>



<a name="222990993"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Promotion%20of%20fallible%20operations%20%28division%2C%20indexing%29/near/222990993" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Promotion.20of.20fallible.20operations.20(division.2C.20indexing).html#222990993">(Jan 16 2021 at 16:35)</a>:</h4>
<p>in contrast, <code>Div</code> is <em>UB</em> when the RHS is 0!</p>



<a name="222991016"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Promotion%20of%20fallible%20operations%20%28division%2C%20indexing%29/near/222991016" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Promotion.20of.20fallible.20operations.20(division.2C.20indexing).html#222991016">(Jan 16 2021 at 16:35)</a>:</h4>
<p>correspondingly, the MIR that's generated around them is also very different</p>



<a name="222991074"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Promotion%20of%20fallible%20operations%20%28division%2C%20indexing%29/near/222991074" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Promotion.20of.20fallible.20operations.20(division.2C.20indexing).html#222991074">(Jan 16 2021 at 16:36)</a>:</h4>
<p>all this reflects that div-by-0 and overflow are just very different kinds of "failure" -- the latter isn't a failure at all, as far as MIR is concerned, it's just "linted against" so to speak</p>



<a name="222991285"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Promotion%20of%20fallible%20operations%20%28division%2C%20indexing%29/near/222991285" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Promotion.20of.20fallible.20operations.20(division.2C.20indexing).html#222991285">(Jan 16 2021 at 16:40)</a>:</h4>
<p>this might be a reflection of the fact that LLVM does not have a safe division operation (LLVM division is UB on 0) - so to ease lowering of MIR to LLVM, there likewise is no safe division operation in MIR</p>



<a name="222994523"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Promotion%20of%20fallible%20operations%20%28division%2C%20indexing%29/near/222994523" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Promotion.20of.20fallible.20operations.20(division.2C.20indexing).html#222994523">(Jan 16 2021 at 17:36)</a>:</h4>
<p><span class="user-mention silent" data-user-id="120791">RalfJ</span> <a href="#narrow/stream/213817-t-lang/topic/Promotion.20of.20fallible.20operations.20(division.2C.20indexing)/near/222990993">said</a>:</p>
<blockquote>
<p>in contrast, <code>Div</code> is <em>UB</em> when the RHS is 0!</p>
</blockquote>
<p>this is just the current behavior though, there is nothing stopping us from adding <code>CheckedDiv</code>, is there?</p>



<a name="222994553"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Promotion%20of%20fallible%20operations%20%28division%2C%20indexing%29/near/222994553" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Promotion.20of.20fallible.20operations.20(division.2C.20indexing).html#222994553">(Jan 16 2021 at 17:36)</a>:</h4>
<p>i don't mind transforming <code>CheckedDiv</code> back to <code>Div + assert</code> after promoting if that's better for llvm and optimizing</p>



<a name="222994723"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Promotion%20of%20fallible%20operations%20%28division%2C%20indexing%29/near/222994723" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Promotion.20of.20fallible.20operations.20(division.2C.20indexing).html#222994723">(Jan 16 2021 at 17:39)</a>:</h4>
<blockquote>
<p>this is just the current behavior though, there is nothing stopping us from adding CheckedDiv, is there?</p>
</blockquote>
<p>nothing in principle, no. it's just a bigger change -- if we want to go that route, I hope someone else can do the MIR changes required for this, as I don't have the time to write such big PRs currently.</p>



<a name="222994798"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Promotion%20of%20fallible%20operations%20%28division%2C%20indexing%29/near/222994798" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Promotion.20of.20fallible.20operations.20(division.2C.20indexing).html#222994798">(Jan 16 2021 at 17:40)</a>:</h4>
<p><span class="user-mention silent" data-user-id="216206">lcnr</span> <a href="#narrow/stream/213817-t-lang/topic/Promotion.20of.20fallible.20operations.20(division.2C.20indexing)/near/222994553">said</a>:</p>
<blockquote>
<p>i don't mind transforming <code>CheckedDiv</code> back to <code>Div + assert</code> after promoting if that's better for llvm and optimizing</p>
</blockquote>
<p>that's the wrong transformation, <code>CheckedDiv</code> wouldn't assert. it would have to transform to something like</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">if</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="kc">true</span><span class="p">)</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="p">(</span><span class="n">x</span><span class="o">/</span><span class="n">y</span><span class="p">,</span><span class="w"> </span><span class="kc">false</span><span class="p">)</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="222994909"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Promotion%20of%20fallible%20operations%20%28division%2C%20indexing%29/near/222994909" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Promotion.20of.20fallible.20operations.20(division.2C.20indexing).html#222994909">(Jan 16 2021 at 17:42)</a>:</h4>
<p><em>change <code>CheckedDiv, assert</code> to <code>assert, Div</code></em></p>



<a name="222994947"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Promotion%20of%20fallible%20operations%20%28division%2C%20indexing%29/near/222994947" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Promotion.20of.20fallible.20operations.20(division.2C.20indexing).html#222994947">(Jan 16 2021 at 17:43)</a>:</h4>
<blockquote>
<p>as I don't have the time to write such big PRs currently.</p>
</blockquote>
<p>i would prefer us to do it this way and don't mind spending the time to implement this if so desired</p>



<a name="222995097"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Promotion%20of%20fallible%20operations%20%28division%2C%20indexing%29/near/222995097" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Promotion.20of.20fallible.20operations.20(division.2C.20indexing).html#222995097">(Jan 16 2021 at 17:45)</a>:</h4>
<p>Fair. I am fine either way.</p>



<a name="222995198"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Promotion%20of%20fallible%20operations%20%28division%2C%20indexing%29/near/222995198" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Promotion.20of.20fallible.20operations.20(division.2C.20indexing).html#222995198">(Jan 16 2021 at 17:47)</a>:</h4>
<p>FWIW, that would stll leave indexing -- <code>[1,2,3][2]</code> would be promoted but <code>[1,2,3][1+1]</code> would not. it's strange but IMO worth it for the compiler cleanup. (once that PR lands, we can remove all the special casing for const-eval error reporting around promoteds, and we can ICE in codegen when encountering CTFE error instead of having to somehow continue codegen.)</p>



<a name="222996714"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Promotion%20of%20fallible%20operations%20%28division%2C%20indexing%29/near/222996714" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Promotion.20of.20fallible.20operations.20(division.2C.20indexing).html#222996714">(Jan 16 2021 at 18:13)</a>:</h4>
<p>indexing is harder to fix, uuuu</p>



<a name="222997146"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Promotion%20of%20fallible%20operations%20%28division%2C%20indexing%29/near/222997146" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Promotion.20of.20fallible.20operations.20(division.2C.20indexing).html#222997146">(Jan 16 2021 at 18:21)</a>:</h4>
<p>well, considering that i don't know of a way to avoid this for indexing i am fine with you going ahead here. Can always relax the limitations later on afaik <span aria-label="thinking" class="emoji emoji-1f914" role="img" title="thinking">:thinking:</span></p>



<a name="222997831"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Promotion%20of%20fallible%20operations%20%28division%2C%20indexing%29/near/222997831" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Promotion.20of.20fallible.20operations.20(division.2C.20indexing).html#222997831">(Jan 16 2021 at 18:33)</a>:</h4>
<p>we could, in theory, do the same for indexing as division, right? something like</p>
<p><code>if index &lt; LENGTH { Some(arr[index]) } else { None }</code> and then an assert/unwrap</p>



<a name="222997916"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Promotion%20of%20fallible%20operations%20%28division%2C%20indexing%29/near/222997916" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Promotion.20of.20fallible.20operations.20(division.2C.20indexing).html#222997916">(Jan 16 2021 at 18:34)</a>:</h4>
<p>we essentially do that already</p>



<a name="222997931"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Promotion%20of%20fallible%20operations%20%28division%2C%20indexing%29/near/222997931" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Promotion.20of.20fallible.20operations.20(division.2C.20indexing).html#222997931">(Jan 16 2021 at 18:35)</a>:</h4>
<p>but the primitive operation that gets promoted can still fail (the <code>arr[index]</code>)</p>



<a name="222997940"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Promotion%20of%20fallible%20operations%20%28division%2C%20indexing%29/near/222997940" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Promotion.20of.20fallible.20operations.20(division.2C.20indexing).html#222997940">(Jan 16 2021 at 18:35)</a>:</h4>
<p>how so?</p>



<a name="222998020"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Promotion%20of%20fallible%20operations%20%28division%2C%20indexing%29/near/222998020" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Promotion.20of.20fallible.20operations.20(division.2C.20indexing).html#222998020">(Jan 16 2021 at 18:36)</a>:</h4>
<p>when promoting, we do not promote the check... oh I see what you are saying, we would have a literal branch for each index op?</p>



<a name="222998063"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Promotion%20of%20fallible%20operations%20%28division%2C%20indexing%29/near/222998063" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Promotion.20of.20fallible.20operations.20(division.2C.20indexing).html#222998063">(Jan 16 2021 at 18:37)</a>:</h4>
<p>promotion currently can't handle branches, and I worry this would make codegen worse, but it's worth exploring</p>



<a name="222998141"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Promotion%20of%20fallible%20operations%20%28division%2C%20indexing%29/near/222998141" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Promotion.20of.20fallible.20operations.20(division.2C.20indexing).html#222998141">(Jan 16 2021 at 18:38)</a>:</h4>
<p>well, then div as ralf suggested also would not work</p>



<a name="222998158"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Promotion%20of%20fallible%20operations%20%28division%2C%20indexing%29/near/222998158" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Promotion.20of.20fallible.20operations.20(division.2C.20indexing).html#222998158">(Jan 16 2021 at 18:38)</a>:</h4>
<p>so it seems like both are blocked on promotion handling branches, presumably</p>



<a name="222998182"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Promotion%20of%20fallible%20operations%20%28division%2C%20indexing%29/near/222998182" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Promotion.20of.20fallible.20operations.20(division.2C.20indexing).html#222998182">(Jan 16 2021 at 18:39)</a>:</h4>
<p>well... if we had a fallible primitive for div and indexing, then we'd have to only create a branch in llvm</p>



<a name="222998264"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Promotion%20of%20fallible%20operations%20%28division%2C%20indexing%29/near/222998264" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Promotion.20of.20fallible.20operations.20(division.2C.20indexing).html#222998264">(Jan 16 2021 at 18:40)</a>:</h4>
<p>if we can get this to generate the same efficient machine code, then we could do it without promotion handling branches</p>



<a name="222998266"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Promotion%20of%20fallible%20operations%20%28division%2C%20indexing%29/near/222998266" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Promotion.20of.20fallible.20operations.20(division.2C.20indexing).html#222998266">(Jan 16 2021 at 18:40)</a>:</h4>
<p>oh, you mean basically putting the primitive in rustc, rather than lowering to this? sure, that makes sense</p>



<a name="222998271"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Promotion%20of%20fallible%20operations%20%28division%2C%20indexing%29/near/222998271" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Promotion.20of.20fallible.20operations.20(division.2C.20indexing).html#222998271">(Jan 16 2021 at 18:40)</a>:</h4>
<p>yea</p>



<a name="222998285"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Promotion%20of%20fallible%20operations%20%28division%2C%20indexing%29/near/222998285" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Promotion.20of.20fallible.20operations.20(division.2C.20indexing).html#222998285">(Jan 16 2021 at 18:41)</a>:</h4>
<p>We'd have to move indexing out of the projections, but wg-mir-opt planned to do that anyway</p>



<a name="222998313"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Promotion%20of%20fallible%20operations%20%28division%2C%20indexing%29/near/222998313" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Promotion.20of.20fallible.20operations.20(division.2C.20indexing).html#222998313">(Jan 16 2021 at 18:41)</a>:</h4>
<p>I guess at that point indexing could be a binop <span aria-label="grinning" class="emoji emoji-1f600" role="img" title="grinning">:grinning:</span></p>



<a name="222998763"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Promotion%20of%20fallible%20operations%20%28division%2C%20indexing%29/near/222998763" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Promotion.20of.20fallible.20operations.20(division.2C.20indexing).html#222998763">(Jan 16 2021 at 18:49)</a>:</h4>
<p><span class="user-mention silent" data-user-id="116122">simulacrum</span> <a href="#narrow/stream/213817-t-lang/topic/Promotion.20of.20fallible.20operations.20(division.2C.20indexing)/near/222998141">said</a>:</p>
<blockquote>
<p>well, then div as ralf suggested also would not work</p>
</blockquote>
<p>the Div would be a MIR primitive, so there'd be no branches</p>



<a name="222998857"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Promotion%20of%20fallible%20operations%20%28division%2C%20indexing%29/near/222998857" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Promotion.20of.20fallible.20operations.20(division.2C.20indexing).html#222998857">(Jan 16 2021 at 18:50)</a>:</h4>
<p>but yeah for indexing projections it's much less clear how to make that an infallibe operation plus an <em>after the fact</em> check (that's the key point here; overflow already has after-the-fact checks, but div and indexing currently <em>requires</em> upfront checks)</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>